//
//  HLTorderTableViewController.m
//  huiliantong
//
//  Created by hlt on 15/9/8.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "HLTorderTableViewController.h"
#import "HLTorderTableViewCell.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "HLTorderinfoTableViewController.h"
@interface HLTorderTableViewController ()


@property NSUInteger count;

@property NSMutableArray *zhifuma;

@end

@implementation HLTorderTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _count=0;
    
    self.zhifuma=[[NSMutableArray alloc]initWithCapacity:10];
    [self setUpTableView];
    
    [self setExtraCellLineHidden:self.tableView];
    _biaozhi=NO;
    
    
    
    [self.navigationItem setTitle:@"订单"];
    
    
    //取消分割线
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


-(void)setExtraCellLineHidden: (UITableView *)tableView

{
    
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
    
    
    
}


-(void)setUpTableView{
    //添加下拉的动画图片
    //设置下拉刷新回调
    [self.tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
    
//    //设置普通状态的动画图片
//    NSMutableArray *idleImages = [NSMutableArray array];
//    for (NSUInteger i = 1; i<=60; ++i) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd",i]];
//        [idleImages addObject:image];
//    }
//    [self.tableView.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];
//    
//    //设置即将刷新状态的动画图片
//    NSMutableArray *refreshingImages = [NSMutableArray array];
//    for (NSInteger i = 1; i<=3; i++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd",i]];
//        [refreshingImages addObject:image];
//    }
//    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling];
//    
//    //设置正在刷新是的动画图片
//    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStateRefreshing];
    
    //马上进入刷新状态
    [self.tableView.gifHeader beginRefreshing];
    
    
    //上拉刷新
    [self.tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    //隐藏状态文字
    //    self.tableView.footer.stateHidden = YES;
    //设置正在刷新的动画
//    self.tableView.gifFooter.refreshingImages = refreshingImages;
    
    
    
    
}



-(void)getFirstPageData{
    _count = 0;
    [self refreshData];
}

-(void)refreshData{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        
        [self getzhifumalist:[NSString stringWithFormat: @"%ld", (long)_type] page:@"0"];
        
        //        [self getMerchantList];
    });
}

-(void)loadMoreData{
    NSLog(@"2222  offset:%zd",_count);
    _count = _count + 10;
    NSLog(@"3333  offset:%zd",_count);
    [self refreshData];
}




-(void) getzhifumalist:(NSString *) type page:(NSString *) page
{
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    
    NSInteger page1=_count/10;
    if (page1>0) {
        //订单列表接口 (&page=0 &username =18285117086 &shop_order_statue = 1 (订单状态))
        NSString *url=[orderlist stringByAppendingFormat:@"&username=%@&shop_order_statue=%@&page=%ld",acc.custPhone,type,(long)page1];
        [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
            
            [self.zhifuma addObjectsFromArray:ary];
            
            NSUInteger cunt=[ary count];
            _count=_count-10+cunt;
            
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
        } faliure:^(NSString *error) {
            
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
        }];
        
        
    }
    else
    {
        NSString *url=[orderlist stringByAppendingFormat:@"&username=%@&shop_order_statue=%@&page=%ld",acc.custPhone,type,(long)page1];
        [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
            
            NSUInteger cunt=[ary count];
            _count=cunt;
            [self.zhifuma removeAllObjects];
            [self.zhifuma addObjectsFromArray:ary];
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
            [self.tableView reloadData];
            
        } faliure:^(NSString *error) {
            
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
        }];
        
        
    }
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.zhifuma.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //    static NSString *identify1 = @"HLTloginTableViewCell";
    static NSString *identify1 = @"HLTorderTableViewCell";
    
    //    HLTloginTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify1];
    HLTorderTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:identify1];
    if(cell == nil)
    {
        //        cell= [[[NSBundle mainBundle]loadNibNamed:@"HLTloginTableViewCell" owner:nil options:nil] firstObject];
        cell= [[[NSBundle mainBundle]loadNibNamed:@"HLTorderTableViewCell" owner:nil options:nil] firstObject];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
//    cell.shopimage ;
    
    NSString *imageUrl =  [imageurl stringByAppendingString:self.zhifuma[indexPath.row][@"image"]];
    [cell.shopimage setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"bg_customReview_image_default"]];
    
    cell.jine.text=self.zhifuma[indexPath.row][@"money_paid"];
    cell.shopname.text=self.zhifuma[indexPath.row][@"shop_name"];
    cell.dingdanhao.text=self.zhifuma[indexPath.row][@"order_sn"];
    cell.date.text=self.zhifuma[indexPath.row][@"add_time"];
    
    
    
    NSString *state;
    if ([self.zhifuma[indexPath.row][@"shop_order_statue"] isEqual:@"1"]) {
        state=@"可退款";
        
    }
    else if([self.zhifuma[indexPath.row][@"shop_order_statue"] isEqual:@"2"])
    {
        state=@"退款中";
        //隐藏确认支付按钮
        
    }else if([self.zhifuma[indexPath.row][@"shop_order_statue"] isEqual:@"3"])
    {
        state=@"退款成功";
        //隐藏确认支付按钮
        cell.accessoryType=UITableViewCellAccessoryNone;
        
    }else if([self.zhifuma[indexPath.row][@"shop_order_statue"] isEqual:@"4"])
    {
        state=@"交易完成";
        //隐藏确认支付按钮
        
        cell.accessoryType=UITableViewCellAccessoryNone;
        
    }else if([self.zhifuma[indexPath.row][@"shop_order_statue"] isEqual:@"5"])
    {
        state=@"拒绝退款,请联系商家协商解决";
        //隐藏确认支付按钮
        
    }
    cell.state.text=state;
    
    
//    NSString *youxiaodate=[NSString stringWithFormat:@"%@ 至 %@",self.zhifuma[indexPath.row][@"start_date"],self.zhifuma[indexPath.row][@"end_date"]];
//    cell.youxiaodate.text=youxiaodate;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        return 100;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
//     if([self.zhifuma[indexPath.row][@"shop_order_statue"] isEqual:@"1"]||[self.zhifuma[indexPath.row][@"shop_order_statue"] isEqual:@"2"])
//     {
    
         
         UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
         HLTorderinfoTableViewController *orderinfo = [storyBoard instantiateViewControllerWithIdentifier:@"HLTorderinfoTableViewController"];
         orderinfo.orderinfo=self.zhifuma[indexPath.row];
         orderinfo.parent=self;
         [self.parent.navigationController pushViewController:orderinfo animated:YES];
         
//     }
    
    
}



- (void) reloadtable
{
    _count=0;
            [self getzhifumalist:[NSString stringWithFormat: @"%ld", (long)_type] page:@"0"];
    
    
}






@end
